package com.shop.shoporder.service.impl;

import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.entity.order.CartList;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.entity.order.OrderAddress;
import com.shop.shoporder.mapper.order.CartMapper;
import com.shop.shoporder.mapper.order.OrderAddressMapper;
import com.shop.shoporder.service.OrderAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("orderAddressService")
public class OrderAddressServiceImpl implements OrderAddressService {
    @Autowired
    OrderAddressMapper orderAddressMapper;
    @Autowired
    CartMapper cartMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return orderAddressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(OrderAddress record) {
        return orderAddressMapper.insert(record);
    }

    @Override
    public int insertSelective(OrderAddress record) {
        return orderAddressMapper.insertSelective(record);
    }

    @Override
    public OrderAddress selectByPrimaryKey(Integer id) {
        return orderAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(OrderAddress record) {
        return orderAddressMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(OrderAddress record) {
        return orderAddressMapper.updateByPrimaryKey(record);
    }

    @Override
    public OrderAddress selectByOrderAddress(OrderAddress record) {
        return orderAddressMapper.selectByOrderAddress(record);
    }

    @Override
    public Response adminEditAddress(Map<String, String> map, HttpServletRequest request) throws Exception {
        Integer addressId=0;
        Integer cityId=0;
        Integer provinceId=0;
        Integer areaId=0;
        String logName="";
        String phone="";
        String detailAddr="";
        String remark="";
        String orderNo="";
        Integer logType=1;
        String logGoodsInfo="";
        Order order=new Order();
        List<CartList> orderList=new ArrayList<>();
        if(map!=null) {
            addressId = InterfaceVer.VerNumber(map.get("addressId"));
            cityId = InterfaceVer.VerNumber(map.get("cityId"));
            provinceId = InterfaceVer.VerNumber(map.get("provinceId"));
            areaId = InterfaceVer.VerNumber(map.get("areaId"));
            logName = InterfaceVer.VerStr(map.get("logName"));
            phone = InterfaceVer.VerStr(map.get("phone"));
            detailAddr = InterfaceVer.VerStr(map.get("detailAddr"));
            remark = InterfaceVer.VerStr(map.get("remark"));
            orderNo = InterfaceVer.VerStr(map.get("orderNo"));
            logType = InterfaceVer.VerNumber(map.get("logType"));
            logGoodsInfo = InterfaceVer.VerStr(map.get("logGoodsInfo"));
            System.out.println("logType:"+logType+" logGoodsInfo:"+logGoodsInfo);
            OrderAddress orderAddress=orderAddressMapper.selectByPrimaryKey(addressId);
            if(orderAddress!=null){
                orderAddress.setProvinceId(provinceId);
                orderAddress.setCityId(cityId);
                orderAddress.setAreaId(areaId);
                orderAddress.setName(logName);
                orderAddress.setPhone(phone);
                orderAddress.setDetailAddr(detailAddr);
                orderAddress.setRemark(remark);
                orderAddress.setProvinceId(provinceId);
                int res=orderAddressMapper.updateByPrimaryKeySelective(orderAddress);
                if(res>0){
//                    if(logType==2){
                        String logGoodsInfoStr[]=logGoodsInfo.split(",");
                        List<String> list=new ArrayList<>();
                        for (String str:logGoodsInfoStr) {
                            if(!str.equals("")&&!str.equals(",")){
                                list.add(str);
                            }
                        }
                        int res1=cartMapper.upCartLogType(list,logType);
                        if(res1>0){
                            return new Response(MessageConstants.MSG_SUCCESS,null,0,"修改成功!",request);
                        }else {
                            throw new RuntimeException();
                        }
//                    }else {
//                        return new Response(MessageConstants.MSG_SUCCESS,null,0,"修改成功!",request);
//                    }
                }else {
                    throw new RuntimeException();
                    //return new Response(MessageConstants.MSG_UP_ERROR,null,0,"修改失败!",request);
                }
            }else {
                return new Response(MessageConstants.MSG_UP_ERROR,null,0,"修改失败!",request);
            }
        }else {
            return new Response(MessageConstants.MSG_UP_ERROR,null,0,"修改失败!",request);
        }
    }
}
